// pages/msg/msg.js
import config from "../../config.js";
const base = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {


    // 消息列表
    message: [
      {
        msgType:"text",
        icon: "https://app.longchg.com/gudongcar/images/app/avator.jpg",
        title: "订单助手",
        from:"other",
        detail: "管家您好！我的车发生交通事故需要您这边协助处理一下，我看有个代维修功能。",
        time: "12:30",
      }, {
        msgType: "text",
        icon: "../../image/myheader.png",
        title: "菇咚公告",
        from: "me",
        detail: "您好！我是菇咚好车管家0023号，我现在需要确认一下车况，请您把现场的照片或视频拍！感谢您的信任！",
        time: "12:30",
      }, {
        msgType: "text",
        icon: "https://app.longchg.com/gudongcar/images/app/avator.jpg",
        title: "活动消息",
        from: "other",
        detail: "好的，稍等！",
        time: "12:30",
      }
      , {
        msgType: "image",
        icon: "https://app.longchg.com/gudongcar/images/app/avator.jpg",
        image: "../../image/car.jpg",
        title: "活动消息",
        from: "other",
        detail: "好的，稍等！",
        time: "12:30",
      }
      , {
        msgType: "text",
        icon: "https://app.longchg.com/gudongcar/images/app/avator.jpg",
        title: "菇咚助手",
        detail: "不是特别严重，前保险杠撞坏了，有几块凹进去的区域，我现在拍视频发给您！！",
        time: "12:30",
      }, {
        msgType: "text",
        icon: "../../image/myheader.png",
        title: "私信消息",
        from: "me",
        detail: "收到，我们尽快会赶过去处理，感谢您的耐心配合！",
        time: "12:30",
      }, {
        msgType: "text",
        icon: "../../image/myheader.png",
        title: "私信消息",
        from: "me",
        detail: " 我已经出发赶往昆明路一段23号处。。。",
        time: "12:30",
      }, {
        msgType: "image",
        icon: "https://app.longchg.com/gudongcar/images/app/avator.jpg",
        image: "../../image/insurance.png",
        title: "活动消息",
        from: "me",
        detail: "好的，稍等！",
        time: "12:30",
      }
    ],



    msg:[],
    isEnd:false,
    page:0,
    typeId:0,
  },


  //加载更多消息
  loadMore:function(){
    var that = this;

    if(this.data.isEnd){
      return false;
    }else{
      wx.request({
        url: base.getUserNoticeList,
        header: {
          "Content-Type": "application/x-www-form-urlencoded",
          "Cookie": wx.getStorageSync("session")
        },
        data: {
          page: that.data.page,
          size: 10,
          typeId: that.data.typeId,
          flag:1
        },
        success: function (res) {
          console.log(res);
          wx.hideLoading();
          if (res.statusCode == 200) {
            var arr = that.data.msg;
            res.data.data.forEach(function (value) {
              arr.push(value);
            })
            if (res.data.data.length < 10) {
              that.setData({
                isEnd: true
              })
            }
            that.setData({
              msg: arr,
              page: that.data.page + 1
            })
          } else {
            wx.showToast({
              title: '消息获取失败',
              icon: "none"
            })
          }
        }
      })
    }
    

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    var typeId = options.id;
    this.setData({
      typeId:typeId
    })


    wx.showLoading({
      title: '请稍后',
    })
    wx.request({
      url: base.getUserNoticeList,
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Cookie": wx.getStorageSync("session")
      },
      data: {
        page: 0,
        size: 10,
        typeId:that.data.typeId,
        flag: 1
      },
      success: function (res) {
        console.log(res);
        wx.hideLoading();
        if(res.statusCode == 200){
          var arr = that.data.msg;
          res.data.data.forEach(function(value){
            arr.push(value);
          })
          if(res.data.data.length<10){
            that.setData({
              isEnd:true
            })
          }
          that.setData({
            msg: arr,
            page:that.data.page+1
          })
        }else{
          wx.showToast({
            title: '消息获取失败',
            icon:"none"
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      console.log("下拉刷新");
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})